package com.qm.console.service.appversion.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qm.console.component.util.HttpInvokeUtil;
import com.qm.console.controller.dto.BaseRsp;
import com.qm.console.pubdef.constant.UrlConstant;
import com.qm.console.pubdef.dto.appversion.AppVersionUpdateAdminDTO;
import com.qm.console.service.appversion.AppVersionService;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class AppVersionServiceImpl implements AppVersionService {
    private final String AppVersionAdminUrl="/appversion";

    @Override
    public BaseRsp listAppVersionAdmin(Map<String, String> paramMap) {
        String url= UrlConstant.ApiServerHost+AppVersionAdminUrl+"/versions";
        String body= HttpInvokeUtil.httpGet(url,paramMap, null);
        BaseRsp rsp= JSONObject.parseObject(body,BaseRsp.class);
        Object rspData=rsp.getData();
        final JSONObject data=(JSONObject) rspData;
        if(data.containsKey("list")){
            JSONArray list= data.getJSONArray("list");
            for(int i=0;i<list.size();i++){
                JSONObject item=list.getJSONObject(i);
                String id=item.getString("id");
                if(id!=null){
                    item.put("id",id);
                }
            }
        }
        return rsp;
    }

    @Override
    public BaseRsp updateAppVersionAdmin(AppVersionUpdateAdminDTO updateDTO){
        String url= UrlConstant.ApiServerHost+AppVersionAdminUrl+"/versions";
        String req= JSONObject.toJSONString(updateDTO);
        String body=HttpInvokeUtil.httpPostJson(url,req,UrlConstant.DefaultEncode,null);
        BaseRsp rsp= JSONObject.parseObject(body,BaseRsp.class);
        return rsp;
    }
    @Override
    public BaseRsp DeleteAppVersionAdmin(String params){
        JSONObject req=JSONObject.parseObject(params);
        String url= UrlConstant.ApiServerHost+AppVersionAdminUrl+"/versions/"+req.get("id");
        String body=HttpInvokeUtil.httpDelete(url,null);
        BaseRsp rsp=JSONObject.parseObject(body,BaseRsp.class);
        return rsp;
    }
}
